using System;
using System.ServiceModel;
using System.ServiceModel.Dispatcher;

namespace Granite.Wcf.PocoService
{
	/// <summary>
	/// This instance provider returns the associated object each time an instance is requested
	/// </summary>
	class SingletonInstanceProvider : IInstanceProvider
	{
		readonly object m_Singleton;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="singleton"></param>
		public SingletonInstanceProvider(object singleton)
		{
			m_Singleton = singleton;
		}

		/// <summary>
		/// Returns a service object given the specified <see cref="T:System.ServiceModel.InstanceContext" /> object.
		/// </summary>
		/// <returns>
		/// A user-defined service object.
		/// </returns>
		/// <param name="instanceContext">
		/// The current <see cref="T:System.ServiceModel.InstanceContext" /> object.
		/// </param>
		public object GetInstance(InstanceContext instanceContext)
		{
			return m_Singleton;
		}

		/// <summary>
		/// Returns a service object given the specified <see cref="T:System.ServiceModel.InstanceContext" /> object.
		/// </summary>
		/// <returns>
		/// The service object.
		/// </returns>
		/// <param name="instanceContext">
		/// The current <see cref="T:System.ServiceModel.InstanceContext" /> object.
		/// </param>
		/// <param name="message">
		/// The message that triggered the creation of a service object.
		/// </param>
		public object GetInstance(InstanceContext instanceContext, System.ServiceModel.Channels.Message message)
		{
			return m_Singleton;
		}

		/// <summary>
		/// Called when an <see cref="T:System.ServiceModel.InstanceContext" /> object recycles a service object.
		/// </summary>
		/// <param name="instanceContext">
		/// The service's instance context.
		/// </param>
		/// <param name="instance">
		/// The service object to be recycled.
		/// </param>
		public void ReleaseInstance(InstanceContext instanceContext, object instance)
		{
			//NOP
		}

	}
}
